//
//  RestaurantSectionController.swift
//  FeastBooking
//
//  Created by Imon on 2017/7/7.
//  Copyright © 2017年 Imon. All rights reserved.
//

import Foundation
import UIKit
import IGListKit

protocol RestaurantCollectionSectionDelegate: class {
    func detailPage(of restaurant: RestaurantSummaryResponse!)
}

class RestaurantSectionController: ListSectionController {

    var restaurant: RestaurantSummaryResponse!
    
    var delegate: RestaurantCollectionSectionDelegate?
    
    var loadedImage: UIImage?
    
    let defaultImage = UIImage(named: "placeholder")
    
    override init() {
        super.init()
        inset = UIEdgeInsets(top: 10, left: 15, bottom: 0, right: 15)
    }
    
    override func numberOfItems() -> Int {
        return 1
    }
    
    override func sizeForItem(at index: Int) -> CGSize {
        guard let context = collectionContext else {return .zero}
        let width = context.containerSize.width
        return CGSize(width: width - 30, height: 130)
    }
    override func cellForItem(at index: Int) -> UICollectionViewCell {
        let cell = collectionContext!.dequeueReusableCell(of: RestaurantCollectionViewCell.self, for: self, at: index)
        if let cell = cell as? RestaurantCollectionViewCell {
            cell.initValue(restaurant: restaurant)
            cell.profilePic.image = defaultImage
            if let url = restaurant.pictureUrl {
                if url != "" {
                    let url = URL(string: url)
                    cell.profilePic.kf.setImage(with: url, placeholder: defaultImage)
                } else {
                    cell.profilePic.image = defaultImage
                }
            } else {
                cell.profilePic.image = defaultImage
            }
            cell.layoutSubviews()
        }
        return cell
    }
    override func didUpdate(to object: Any) {
        restaurant = object as? RestaurantSummaryResponse
    }
    
    override func didSelectItem(at index: Int) {
        delegate?.detailPage(of: restaurant)
    }
}
